package b3.model
{
	import b3.config.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.util.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class GiftBoxModel extends Model
	{
		public var objectIndex:int;
		public var objectNum:int;
		
		public function GiftBoxModel(objIndex:int, objNum:int)
		{
			super();
			objectIndex = objIndex;
			objectNum = objNum;
		}
		
		//使用
		public function useGiftBox():void
		{
			var giftBoxConfig:GiftBoxConfig = Config.objConfigs[objectIndex] as GiftBoxConfig;
			var giftObjects:Array = new Array();
			for (var i:int = 0; i < giftBoxConfig.gifts.length / 2; i++)
			{
				var obj:Obj = new Obj();
				obj.objectIndex = giftBoxConfig.gifts[2 * i];
				obj.objectNum = giftBoxConfig.gifts[2 * i + 1] * objectNum;
				giftObjects.push(obj);
			}
			obj = new Obj();
			obj.objectIndex = this.objectIndex;
			obj.objectNum = -this.objectNum;
		 	var serverRequest:ServerRequest = new ServerRequest(ServerEvent.USE_GIFT_BOX, {obj:obj, giftObjects: giftObjects});
			serverRequest.setHandleFunc(handleUseGiftCard);
			serverRequest.post();
		}
		
		private function handleUseGiftCard(data:Object):void
		{
			var me:Player = PlayerManager.getInstance().me; 
			me.packageModel.handleLoadObjects(data['package']);
	 		var event:ModelEvent = new ModelEvent(ModelEvent.USE_GIFT_BOX);
	 		event.data = data;
	 		dispatchEvent(event);
		}
	}
}